package com.loong.web.gp4j.view.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.loong.tool.lang.Strings;
import com.loong.tool.secret.base.Base64;
import com.loong.tool.secret.base.UTF8;
import com.loong.tool.secret.exception.InvalidCodeException;
import com.loong.web.gp4j.bean.param.user.LoginParam;
import com.loong.web.gp4j.bean.param.user.RepwdParam;
import com.loong.web.gp4j.service.error.ErrorService;
import com.loong.web.gp4j.service.user.UserLoginService;
import com.loong.web.gp4j.service.user.UserService;
import com.loong.web.suppert.collection.valid.ErrorList;
import com.loong.web.suppert.collection.valid.ValidAdapter;
import com.loong.web.suppert.intercept.auth.AuthType;
import com.loong.web.suppert.intercept.statistic.Statistic;

/**
 * 用户登录资源
 *
 * @author 张成轩
 */
@Controller
public class LoginViewMapping {

	@Resource
	private ErrorService errorService;

	@Resource
	private UserLoginService userLoginService;
	@Resource
	private UserService userService;

	/**
	 * 登录页面
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @return 视图
	 */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	@AuthType
	public ModelAndView login(HttpServletRequest request, LoginParam param) {

		ModelAndView model = new ModelAndView("gp4j/login/login");
		model.addObject("hasVc", userLoginService.hasVc(request));
		return model;
	}

	/**
	 * 用户登录页面提交
	 * 
	 * @param request 请求
	 * @param response 响应
	 * @param param 参数
	 * @param valid 校验器
	 * @return 视图
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@AuthType
	public ModelAndView loginSubmit(HttpServletRequest request, HttpServletResponse response, @Valid LoginParam param,
			BindingResult valid) {

		if (!valid.hasErrors()) {
			// 错误集合
			ErrorList errorList = new ErrorList();
			// 调用服务层
			boolean succ = userLoginService.login(request, response, param, errorList);
			// 判断是否包含错误信息
			if (succ) {
				// 返回
				if (Strings.isNotNull(param.getBack()))
					try {
						return new ModelAndView("redirect:" + UTF8.newString(Base64.decode(param.getBack())));
					} catch (InvalidCodeException e) {
						errorService.error(e);
					}
				return new ModelAndView("redirect:/");
			} else
				ValidAdapter.convert(param, errorList, valid);
		}
		return login(request, param);
	}

	/**
	 * 用户注销页面
	 * 
	 * @param request 请求
	 * @param response 响应
	 * @param back 返回路径
	 * @return 视图
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	@AuthType
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, String back) {

		userLoginService.logout(request, response);

		ModelAndView model = new ModelAndView("gp4j/login/logout");
		// 返回路径
		if (Strings.isNotNull(back))
			try {
				model.addObject("url", UTF8.newString(Base64.decode(back)));
			} catch (InvalidCodeException e) {
				errorService.error(e);
			}
		return model;
	}

	/**
	 * 获取验证码图片
	 * 
	 * @param request 请求
	 * @param response 响应
	 */
	@RequestMapping(value = "login/vc", method = RequestMethod.GET)
	@ResponseBody
	@AuthType
	public void vc(HttpServletRequest request, HttpServletResponse response) {

		userLoginService.vc(request, response);
	}

	/**
	 * 忘记密码页面
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @return 视图
	 */
	@RequestMapping(value = "login/repwd", method = RequestMethod.GET)
	@AuthType
	@Statistic("忘记密码")
	public ModelAndView repwd(HttpServletRequest request, RepwdParam param) {

		return new ModelAndView("gp4j/login/repwd");
	}

	/**
	 * 忘记密码页面
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param valid 校验器
	 * @return 视图
	 */
	@RequestMapping(value = "login/repwd", method = RequestMethod.POST)
	@AuthType
	public ModelAndView repwdSubmit(HttpServletRequest request, @Valid RepwdParam param, BindingResult valid) {

		ModelAndView model = new ModelAndView("gp4j/login/repwd");
		if (!valid.hasErrors()) {
			// 错误集合
			ErrorList errorList = new ErrorList();
			// 调用服务层
			boolean succ = userService.rePassword(param, errorList);
			// 判断是否包含错误信息
			if (!succ)
				ValidAdapter.convert(param, errorList, valid);
			model.addObject("succ", succ);
		}
		return model;
	}
}
